#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct privacy   /*定义结构体*/
{
    int num;              /*编号*/
    char location[100];   /*账号位置*/
    char describe[100];   /*账号形容*/
    char name[100];       /*账户名*/
    char password[100];   /*账户密码*/
}PRI ;                /*结构体简称*/
int login();             /*程序密码验证*/
int menu_select();        /*菜单选择*/
int ID_input(PRI *ID,int n);          /*账号信息录入*/
void ID_modify(PRI *ID,int n);         /*账号信息修改*/
int ID_delete(PRI *ID,int n);         /*账户信息删除*/
void ID_search(PRI *ID,int n);         /*账户信息查询*/
void ID_output(PRI *ID,int n);         /*账户信息输出*/
void filewrite(PRI *ID,int n);         /*信息存入文件*/
int fileread(PRI *ID,int n);          /*文件信息导入*/


int main()
{
    int n=0;
    PRI ID[100];
    if(login()==0)
        return 0;
    while(1){
        switch(menu_select())
        {
            case 1:
                n=ID_input(ID, n);
                break;

            case 2:
                ID_modify(ID,n);
                break;

            case 3:
                n=ID_delete(ID, n);
                break;

            case 4:
                ID_search(ID,n);
                break;
            case 5:
                ID_output(ID,n);
                break;
            case 6:
                filewrite(ID,n);
                break;
            case 7:
                n=fileread(ID, n);
                break;
            case 8:
                printf("你已成功退出系统\n");
                return 0;
        }
    }
    return 0;
}


int login()          /*程序密码验证*/
{
    int i;
    char key[20];
    printf("********程序密码验证***********\n");
    for(i=0;i<3;i++){
        scanf("%s",key);
        if(strcmp(key,"2105060307")==0){
                printf("口令正确\n");
                printf("进入隐私管理系统\n");
            return 1;
        }
        else
            printf("口令错误，请重新输入，还有%d次机会\n",2-i);
    }
    if(i==3){
        printf("你是非法用户\n");
        return 0;
}
}


int menu_select()     /*菜单选择*/
{
    int n;
    printf("****************欢迎进入隐私管理系统****************\n");
    printf("           菜单选择\n");
    printf("        1、账号信息录入\n");
    printf("        2、账户信息修改\n");
    printf("        3、账户信息删除\n");
    printf("        4、账户信息查询\n");
    printf("        5、账户信息输出\n");
    printf("        6、信息录入\n");
    printf("        7、文件导入信息\n");
    printf("        8、退出系统\n");
    printf("****************************************************\n");
    printf("      请选择功能编号1-8\n");
    scanf("%d",&n);
    return n;
}


int ID_input( PRI *ID,int n)          /*账号信息录入*/
{
    int i,N;
    printf("请输入你要输入的数据组数\n");
    scanf("%d",&N);
    printf("%-5s%-25s%-20s%-20s%-10s\n","编号","账号位置","账号形容","账号名","账号密码");
    for(i=1;i<=N;i++){
        scanf("%d",&ID[n+i].num);
        scanf("%s",ID[n+i].location);
        scanf("%s",ID[n+i].describe);
        scanf("%s",ID[n+i].name);
        scanf("%s",ID[n+i].password);
    }
    printf("\n\n\n");
    return N+n;
}



void ID_modify(PRI *ID,int n)
{
    int m,q,i,flag=0;
    printf("请输入你要修改的账号的编号:");
   while(scanf("%d",&m)){
        for(i=1;i<=n;i++){
            if(ID[i].num==m){
                flag=1;
                q=i;
                break;
        }}
        if(i==n+1){
            flag=0;
            printf("编号不存在，请重新输入\n");
        }
        if(flag)
            break;
    }
    printf("请输入你要修改的数据：\n");
        scanf("%d",&ID[m].num);
        scanf("%s",ID[m].location);
        scanf("%s",ID[m].describe);
        scanf("%s",ID[m].name);
        scanf("%s",ID[m].password);
    printf("修改已完成\n");

}


int ID_delete(PRI *ID,int n)
{
    int m,i,flag=0,q;
    printf("请输入你要删除的账户编号：");
    while(scanf("%d",&m)){
        for(i=1;i<=n;i++){
            if(ID[i].num==m){
                flag=1;
                q=i;
                break;
        }}
        if(i==n+1){
            flag=0;
            printf("编号不存在，请重新输入\n");
        }
        if(flag)
            break;
    }
    for(i=q;i<=n-1;i++)
        ID[i]=ID[i+1];
    printf("你已删除账号编号为  %d  的数据\n",m);
    return n-1;
}


void ID_search(PRI *ID,int n)
{
    int m,i,q,flag=0;
    printf("请输入你要查询的账号编号:");
    while(scanf("%d",&m)){
        for(i=1;i<=n;i++){
            if(ID[i].num==m){
                flag=1;
                q=i;
                break;
        }}
        if(i==n+1){
            flag=0;
            printf("编号不存在，请重新输入\n");
        }
        if(flag)
            break;
    }
    printf("%-5s%-25s%-20s%-20s%-10s\n","编号","账号位置","账号形容","账号名","账号密码");
    printf("%-5d",ID[q].num);
    printf("%-25s",ID[q].location);
    printf("%-20s",ID[q].describe);
    printf("%-20s",ID[q].name);
    printf("%-10s\n",ID[q].password);
    printf("******************************************\n");
    printf("\n\n\n");
}

void ID_output(PRI *ID,int n)
{
    int i;
     printf("%-5s%-25s%-20s%-15s%-10s\n","编号","账号位置","账号形容","账号名","账号密码");
    for(i=1;i<=n;i++){
    printf("%-5d",ID[i].num);
    printf("%-25s",ID[i].location);
    printf("%-20s",ID[i].describe);
    printf("%-20s",ID[i].name);
    printf("%-10s\n",ID[i].password);
    }
    printf("******************************************\n");
    printf("\n\n\n");
}


int fileread(PRI *ID,int n)
{
    int i=0,j;
   FILE *fp;                                      /* 定义指向文件的指针*/
   char infile[40];                               /*保存文件名*/
   printf("请输入导入文件名，例如：G:\\f1\\score.txt:\n");
   scanf("%s",infile);                            /*输入文件名*/
   if((fp=fopen(infile,"rb"))==NULL)              /*打开一个二进制文件，为读方式*/
   {
		printf("文件打开失败！\n");
		return 0;
   }
   i=0;
   while(!feof(fp))                               /*循环读数据直到文件尾结束*/
   {
	   if(1!=fread(&ID[i],sizeof(PRI),1,fp))
			break;                                       /*如果没读到数据，跳出循环*/
	   i++;
   }
	fclose(fp);                                    /*关闭文件*/
	printf("已经成功从文件%s导入数据!!!\n",infile);
	return i;
}




void filewrite(PRI *ID,int n)
{
    FILE *fp;
    int i=0,j;
    char outfile[50];
    printf("请输入导出文件名，例:G:\\f1\\score.txt:\n\n");
    scanf("%s",outfile);
    if((fp=fopen(outfile,"w"))==NULL)
    {
        printf("文件打开失败\n");
        exit(1);
    }
    else
    {
        for(; i<n; i++)
        {
            fputc(ID[i].num,fp);
            fputs(ID[i].location,fp);
            fputs(ID[i].describe,fp);
            fputs(ID[i].name,fp);
            int j;
            for(j=0; j<strlen(ID[i].password); j++)
                ID[i].password[j]++;     //加密为字符+1 0
            fputs(ID[i].password,fp);
        }}
}
